package com.wfl.weixinqr.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qrcode.QRCodeUtil;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import java.util.Random;
import java.io.OutputStream;
@Slf4j
@Controller
public class ToolController {

    @RequestMapping("/print")
    public void printWx(HttpServletRequest request, HttpServletResponse response){
        System.out.println("test 01");
        System.out.println("test 01");
        //通过request获取页面表单上提交的数据
        String line = request.getParameter("content");

        //参数1就是二维码内容   参数2生成的图片路径
//        QRCodeUtil.encode(line,"./qr.jpg");
        //参数1:二维码内容   2:二维码中间的logo图  3:生成的图片路径 4:是否压缩logo图的尺寸，保持居中
//        QRCodeUtil.encode(line,"./logo.jpg","./qr.jpg",true);
        //参数1:二维码内容   2:二维码中间的logo图  3:字节输出流，生成的图片会通过该流写出 4:是否压缩logo图的尺寸，保持居中
//        OutputStream out = new FileOutputStream("./qr.jpg");
//        QRCodeUtil.encode(line,"./logo.jpg",out,true);

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            QRCodeUtil.encode(line,"./weixinqr/src/main/resources/static/logo.jpg",out,true);
            response.setContentType("image/jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("生成完毕");
    }

    /**
     * 功能描述: 生成二维码 BufferedImage.
     *
     * @param content
     * @param qrWidth
     * @param qrHeight
     * @return java.awt.image.BufferedImage
     * @author zz
     * @date 2019/5/31 9:13
     */
    public static BufferedImage getBufferImage(String content, int qrWidth, int qrHeight) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        log.debug("执行生成二维码 BufferedImage操作");
        return image;
    }

    /**
     * 功能描述:  生成base64格式二维码.
     *
     * @param content  content
     * @param qrWidth  qrWidth
     * @param qrHeight qrHeight
     * @return string
     * @author zz
     * @date 2019/5/31 9:18
     */
    public static String getBase64(String content, int qrWidth, int qrHeight) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            BufferedImage image = getBufferImage(content, qrWidth, qrHeight);
            //转换成png格式的IO流
            ImageIO.write(image, "png", byteArrayOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        byte[] bytes = byteArrayOutputStream.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        String base64 = encoder.encodeBuffer(bytes).trim();
        base64 = "data:image/png;base64," + base64;
        log.debug("执行生成base64格式二维码操作");
        return base64;
    }

    /**
     * 功能描述:  test.
     *
     * @param
     * @return
     * @author zz
     * @date 2019/5/30 19:27
     */
    public static void main(String[] args) throws Exception {
        ImageIO.write(getBufferImage("4545454545", 500, 500), "jpg", new File("d://qrCode.jpg"));
//        System.out.println("--base64--" + getBase64("4545454545", 500, 500));
    }



}
